package com.lite.business.entity.commodities;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lite.business.entity.DateInterval;
import com.lite.business.entity.PriceInterval;
import com.lite.common.entity.QueryConditionWrapper;
import lombok.Builder;
import lombok.Data;

import java.util.Objects;

/**
 * @author Stranger
 * @version 1.0
 * @description: 商品查询包装类
 * @date 2022/10/25 21:06
 */
@Data
@Builder
public class CommodityQueryWrapper extends QueryConditionWrapper<Commodities> {

    private Commodities commodities;

    private PriceInterval priceInterval;

    private DateInterval dateInterval;

    /**
     * 商品查询包装类
     */
    public LambdaQueryWrapper<Commodities> buildWrapper() {

        LambdaQueryWrapper<Commodities> commoditiesLambdaUpdateWrapper = new LambdaQueryWrapper<>();

        Commodities commoditiesWrap = getCommodities();

        if (!Objects.isNull(commoditiesWrap)) {
            if (!Objects.isNull(commoditiesWrap.getName())) {
                commoditiesLambdaUpdateWrapper.eq(Commodities::getName, commoditiesWrap.getName());
            }

            if (!Objects.isNull(commoditiesWrap.getOrigin())) {
                commoditiesLambdaUpdateWrapper.eq(Commodities::getOrigin, commoditiesWrap.getOrigin());
            }
        }

        if (!Objects.isNull(priceInterval)) {
            commoditiesLambdaUpdateWrapper
                    .ge(Commodities::getPrice, priceInterval.getBegin())
                    .le(Commodities::getPrice, priceInterval.getEnd());
        }

        if (!Objects.isNull(dateInterval)) {
            commoditiesLambdaUpdateWrapper
                    .ge(Commodities::getCreateTime, dateInterval.getBegin())
                    .le(Commodities::getCreateTime, dateInterval.getEnd());
        }

        return commoditiesLambdaUpdateWrapper;
    }
}
